package org.xobo.finance.helper.invoice.view;

import java.util.Collection;
import java.util.Map;

import org.malagu.panda.coke.utility.Coke;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xobo.finance.helper.company.entity.Company;
import org.xobo.finance.helper.invoice.domain.InvoiceMonthSummary;
import org.xobo.finance.helper.invoice.entity.Invoice;
import org.xobo.finance.helper.invoice.service.InvoiceCompanyCalcService;
import org.xobo.finance.helper.invoice.service.InvoiceCompanyService;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

@Service
@Transactional(readOnly = true)
public class InvoiceMaintain {
  @DataProvider
  public void loadCompany(Page<Company> page, Criteria criteria, Map<String, Object> parameterMap) {
    Coke.query(Company.class, criteria, parameterMap).paging(page);

    Collection<Company> companyList = page.getEntities();
    invoiceCompanyService.calcCompanyInvoiceAmount(companyList);

  }

  @DataProvider
  public void loadInvoice(Page<Invoice> page, Criteria criteria, Map<String, Object> parameterMap) {
    Coke.query(Invoice.class, criteria, parameterMap).desc("id").paging(page);
  }

  @DataProvider
  public Collection<InvoiceMonthSummary> loadCompanyInvoiceMonthSummary(Long companyId) {
    return invoiceCompanyCalcService.loadCompanyYearMonthAmount(companyId);
  }

  @DataResolver
  @Transactional
  public void saveCompany(Collection<Company> companyList) {
    Coke.save(companyList);
  }

  @DataResolver
  @Transactional
  public void saveInvoice(Collection<Invoice> invoiceList) {
    Coke.save(invoiceList);
  }

  @Autowired
  private InvoiceCompanyService invoiceCompanyService;

  @Autowired
  private InvoiceCompanyCalcService invoiceCompanyCalcService;
}

